<?php

class Term_con extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        //defaultnya akan menampilkan daftar term yang sudah ada, dapat difilter    
        $this->list_term();
    }

    function list_term($filter = '') {
        //menampilkan daftar term yang sudah ada, ada tombol tambah yang akan ke 
        //halaman untuk menambahkan term
        if ($filter == 'Semua') {
            $filter = '';
        }
        if ($filter == '') {
            $query = $this->db->get('term');
        } else {
            $query = $this->db->get_where('term', array('STATUS' => $filter));
        }
        if ($query->num_rows() > 0) {
            //membuat tabel
            $this->load->library('table');
            $this->table->set_template(array('table_open' => '<table border="1" cellpadding="4" cellspacing="3">'));
            $this->table->set_heading('NO', 'TERM', 'TAHUN', 'TGL MULAI', 'BATAS DAFTAR', 'BULAN MULAI', 'BULAN SELESAI', 'STATUS', 'AKSI');
            $i = 1;
            foreach ($query->result() as $entry) {
                $this->table->add_row(
                        $i, $entry->NO_TERM, $entry->TAHUN, $entry->TGL_MULAI, $entry->BATAS_PENDAFTARAN, $entry->BULAN_MULAI, $entry->BULAN_SELESAI, $entry->STATUS, anchor(base_url() . 'admin/term_con/edit_term/' . $entry->ID_TERM, 'Edit') .
                        ' | ' .
                        anchor(base_url() . 'admin/term_con/delete/' . $entry->ID_TERM, 'Delete') .
                        ' | ' .
                        anchor(base_url() . 'admin/term_con/jalankan_term/' . $entry->ID_TERM, 'Jalankan') .
                        ' | ' .
                        anchor(base_url() . 'admin/term_con/upload_brosur/' . $entry->ID_TERM, 'Upload Brosur')
                );
                $i++;
            }
            $display['content'] = $this->table->generate();
        } else {
            $display['content'] = "Belum ada term yang dibuat";
        }
        $data['title'] = "List Term";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/list_term', $display);
        $this->load->view('footer_view');
    }

    function filter() {
        //redirect untuk ke filter
        $filter = $this->input->post('filter');
        redirect(base_url() . 'admin/term_con/list_term/' . $filter);
    }

    function edit_term($id = '') {
        //berguna untuk menambahkan halaman atau mengedit halaman    
        $this->load->library('simple_effect');
        $data['script'] = $this->simple_effect->date_picker('date') . $this->simple_effect->date_picker('date2');
        if ($id == '') {
            $display['status'] = 'insert';
            $display['NO_TERM'] = '';
            $display['TAHUN'] = '';
            $display['TGL_MULAI'] = '';
            $display['BATAS_PENDAFTARAN'] = '';
            $display['STATUS'] = '';
            $display['BULAN_MULAI'] = '';
            $display['BULAN_SELESAI'] = '';
        } else {
            $display['status'] = 'update';
            $query = $this->db->get_where('term', array('ID_TERM' => $id));
            $row = $query->row();
            $display['ID_TERM'] = $id;
            $display['NO_TERM'] = $row->NO_TERM;
            $display['TAHUN'] = $row->TAHUN;
            $display['TGL_MULAI'] = $row->TGL_MULAI;
            $display['BATAS_PENDAFTARAN'] = $row->BATAS_PENDAFTARAN;
            $display['STATUS'] = $row->STATUS;
            $display['BULAN_MULAI'] = $row->BULAN_MULAI;
            $display['BULAN_SELESAI'] = $row->BULAN_SELESAI;
        }
        $data['title'] = "Term";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/input_term', $display);
        $this->load->view('footer_view');
    }

    function insert() {
        //memasukkan ke database    
        $this->db->insert('term', $_POST);
        redirect(base_url() . 'admin/term_con');
    }

    function update() {
        //mengupdate data di database    
        if ($_POST['TGL_MULAI'] == '')
            unset($_POST['TGL_MULAI']);
        if ($_POST['BATAS_PENDAFTARAN'] == '')
            unset($_POST['BATAS_PENDAFTARAN']);

        $this->db->where('ID_TERM', $_POST['ID_TERM']);
        $this->db->update('term', $_POST);
        redirect(base_url() . 'admin/term_con');
    }

    function delete($id) {
        //menghapus    
        $this->db->delete('term', array('ID_TERM' => $id));
        redirect(base_url() . 'admin/term_con');
    }

    function jalankan_term($id) {
        //menjalankan term, efeknya: status term berubah, status CIC card 
        //peserta menjadi valid    
        $this->db->where('ID_TERM', $id);
        $this->db->update('term', array('STATUS' => 'SEDANG'));
        $this->db->update('peserta', array('STATUS_CIC_CARD' => 'TRUE'));
        redirect(base_url() . 'admin/term_con');
    }

    function upload_brosur($id_term){
        $data['id_term']=$id_term;
        $this->load->view('admin/admin_header_view');
        $this->load->view('admin/term_brosur',$data);
        $this->load->view('footer_view');
    }
    
    function upload_term_process(){
        $id_term=$_POST['id_term'];
        $term = $this->db->get_where('term',array('ID_TERM'=>$id_term))->row_array();
        $nama_term = 'term-'.$term['NO_TERM'].'-'.$term['TAHUN'];
        
        if(!empty($_FILES['price']['name'])){
            $pecah = explode('.', $_FILES['price']['name']);			
            $file_ext =  $pecah[count($pecah)-1];
            $_FILES['price']['name']='pricelist-'.$nama_term.'.'.$file_ext;
            
            $config['upload_path'] = './brosur/';
            $config['overwrite'] = true;
            $config['allowed_types'] = 'pdf';

            $this->load->library('upload', $config);
            if($this->upload->do_upload('price')){
                $this->db->where(array('ID_TERM'=>$id_term));
                $this->db->update('term',array('PRICELIST'=>base_url().'brosur/'.$_FILES['price']['name']));
            }
            
        }
        
        if(!empty($_FILES['product']['name'])){
            $pecah = explode('.', $_FILES['product']['name']);			
            $file_ext =  $pecah[count($pecah)-1];
            $_FILES['product']['name']='productlist-'.$nama_term.'.'.$file_ext;
            
            $config['upload_path'] = './brosur/';
            $config['overwrite'] = true;
            $config['allowed_types'] = 'pdf';

            $this->load->library('upload', $config);
            if($this->upload->do_upload('product')){
                $this->db->where(array('ID_TERM'=>$id_term));
                $this->db->update('term',array('PRODUCTLIST'=>base_url().'brosur/'.$_FILES['product']['name']));
            }
        }
        
        redirect(base_url().'admin/term_con');
        
    }
    
}

?>
